/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2023. All rights reserved.
 */

package service

import (
	"prim-server/dao"
	"prim-server/model"
	"prim-server/util"
	"time"

	"prim-server/logger"

	"github.com/pkg/errors"
)

const (
	OnePayOrderTaskName         = "onePayOrder"
	OnDemandOrderTaskName       = "onDemandOrder"
	OnePayBillTaskName          = "onePayBill"
	OnDemandBillTaskName        = "onDemandBill"
	OnePayResourceFeeTaskName   = "onePayResourceFee"
	OnDemandResourceFeeTaskName = "onDemandResourceFee"

	OnePayOrderTaskId         = 1
	OnDemandOrderTaskId       = 2
	OnePayBillTaskId          = 3
	OnDemandBillTaskId        = 4
	OnePayResourceFeeTaskId   = 5
	OnDemandResourceFeeTaskId = 6

	TaskRunning = true
	TaskStop    = false
)

var (
	onePayOrderTask = &model.TimerTask{
		TaskId:            OnePayOrderTaskId,
		TaskName:          OnePayOrderTaskName,
		TaskRunningStatus: false,
		LastRunningTime:   util.GetUTC(time.Now()),
	}
	onDemandOrderTask = &model.TimerTask{
		TaskId:            OnDemandOrderTaskId,
		TaskName:          OnDemandOrderTaskName,
		TaskRunningStatus: false,
		LastRunningTime:   util.GetUTC(time.Now()),
	}
	onePayBillTask = &model.TimerTask{
		TaskId:            OnePayBillTaskId,
		TaskName:          OnePayBillTaskName,
		TaskRunningStatus: false,
		LastRunningTime:   util.GetUTC(time.Now()),
	}
	onDemandBillTask = &model.TimerTask{
		TaskId:            OnDemandBillTaskId,
		TaskName:          OnDemandBillTaskName,
		TaskRunningStatus: false,
		LastRunningTime:   util.GetUTC(time.Now()),
	}
	onePayResourceFeeTask = &model.TimerTask{
		TaskId:            OnePayResourceFeeTaskId,
		TaskName:          OnePayResourceFeeTaskName,
		TaskRunningStatus: false,
		LastRunningTime:   util.GetUTC(time.Now()),
	}
	onDemandResourceFeeTask = &model.TimerTask{
		TaskId:            OnDemandResourceFeeTaskId,
		TaskName:          OnDemandResourceFeeTaskName,
		TaskRunningStatus: false,
		LastRunningTime:   util.GetUTC(time.Now()),
	}
)

// InitTimerTaskTable 初始化定时任务表
func InitTimerTaskTable() error {
	timerTasks := []*model.TimerTask{
		onePayOrderTask, onDemandOrderTask,
		onePayBillTask, onDemandBillTask,
		onePayResourceFeeTask, onDemandResourceFeeTask,
	}
	for _, initTask := range timerTasks {
		task, err := GetTaskWithTaskId(initTask.TaskId)
		if err != nil {
			logger.Error(err.Error())
			return errors.Errorf("InitTimerTaskTable error: %v", err)
		}

		if task == nil {
			if err := dao.GetTimerTaskDao().InsertTimerTask(initTask); err != nil {
				logger.Error(err.Error())
				return errors.Errorf("InitTimerTaskTable error: %v", err)
			}
		}
	}
	return nil
}

func GetTaskWithTaskId(taskId int) (*model.TimerTask, error) {
	return dao.GetTimerTaskDao().GetTimerTask(taskId)
}

func UpdateTaskWithStatus(taskId int, taskStatus bool) error {
	task, err := GetTaskWithTaskId(taskId)
	if err != nil {
		logger.Error(err.Error())
		return err
	}
	task.TaskRunningStatus = taskStatus
	task.LastRunningTime = util.GetUTC(time.Now())
	err = dao.GetTimerTaskDao().UpdateTimerTask(task)
	if err != nil {
		logger.Error(err.Error())
		return err
	}
	return nil
}

func GetOnePayOrderTaskStatus() (bool, error) {
	task, err := GetTaskWithTaskId(OnePayOrderTaskId)
	if err != nil {
		return true, err
	}
	return task.TaskRunningStatus, nil
}

func GetOnDemandOrderTaskStatus() (bool, error) {
	task, err := GetTaskWithTaskId(OnDemandOrderTaskId)
	if err != nil {
		return true, err
	}
	return task.TaskRunningStatus, nil
}
func UpdateOnePayOrderTaskWithStatus(status bool) error {
	err := UpdateTaskWithStatus(OnePayOrderTaskId, status)
	if err != nil {
		logger.Error(err.Error())
		return err
	}
	return nil
}

func UpdateOnDemandOrderTaskWithStatus(status bool) error {
	err := UpdateTaskWithStatus(OnDemandOrderTaskId, status)
	if err != nil {
		logger.Error(err.Error())
		return err
	}
	return nil
}

func GetOnePayBillTaskStatus() (bool, error) {
	task, err := GetTaskWithTaskId(OnePayBillTaskId)
	if err != nil {
		return true, err
	}
	return task.TaskRunningStatus, nil
}

func GetOnDemandBillTaskStatus() (bool, error) {
	task, err := GetTaskWithTaskId(OnDemandBillTaskId)
	if err != nil {
		return true, err
	}
	return task.TaskRunningStatus, nil
}

func UpdateOnePayBillTaskWithStatus(status bool) error {
	err := UpdateTaskWithStatus(OnePayBillTaskId, status)
	if err != nil {
		logger.Error(err.Error())
		return err
	}
	return nil
}

func UpdateOnDemandBillTaskWithStatus(status bool) error {
	err := UpdateTaskWithStatus(OnDemandBillTaskId, status)
	if err != nil {
		logger.Error(err.Error())
		return err
	}
	return nil
}

func GetOnePayResourceFeeTaskStatus() (bool, error) {
	task, err := GetTaskWithTaskId(OnePayResourceFeeTaskId)
	if err != nil {
		return true, err
	}
	return task.TaskRunningStatus, nil
}

func GetOnDemandResourceFeeTaskStatus() (bool, error) {
	task, err := GetTaskWithTaskId(OnDemandResourceFeeTaskId)
	if err != nil {
		return true, err
	}
	return task.TaskRunningStatus, nil
}

func UpdateOnePayResourceFeeTaskWithStatus(status bool) error {
	err := UpdateTaskWithStatus(OnePayResourceFeeTaskId, status)
	if err != nil {
		logger.Error(err.Error())
		return err
	}
	return nil
}

func UpdateOnDemandResourceFeeTaskWithStatus(status bool) error {
	err := UpdateTaskWithStatus(OnDemandResourceFeeTaskId, status)
	if err != nil {
		logger.Error(err.Error())
		return err
	}
	return nil
}
